local jiangu = fk.CreateSkill{
  name = "steam__jiangu",
  tags={Skill.Family},
}
local U = require "packages/utility/utility"
--宗族判断
local familyMember = function (player, target)
  if target == player then
    return true
  end

  local family = {}
  local familyMap = {
    ["yingchuan_xun"] = {"xunshu", "xunchen", "xuncai", "xuncan", "xunyu", "xunyou"},
    ["chenliu_wu"] = {"wuxian", "wuyi", "wuban", "wukuang", "wuqiao"},
    ["yingchuan_han"] = {"hanshao", "hanrong"},
    ["taiyuan_wang"] = {"wangyun", "wangling", "wangchang", "wanghun", "wanglun", "wangguang", "wangmingshan", "wangshen"},
    ["yingchuan_zhong"] = {"zhongyao", "zhongyu", "zhonghui", "zhongyan"},
    ["hongnong_yang"] = {"yangci", "yangbiao", "yangxiu", "yangzhongh", "yangjun", "yangyan", "yangzhi"},
    ["wujun_lu"] = {"luji", "luxun", "lukang", "luyusheng", "lukai", "lujing"},
    ["jin_cui"] = {"cuiyan","cuijue"},
  }
  for f, members in pairs(familyMap) do
    if table.contains(members, Fk.generals[player.general].trueName) then
      table.insertIfNeed(family, f)
    end
    if table.contains(table.map(members, function (name)
      return "god"..name
    end), Fk.generals[player.general].trueName) then
      table.insertIfNeed(family, f)
    end
    if player.deputyGeneral ~= "" then
      if table.contains(members, Fk.generals[player.deputyGeneral].trueName) then
        table.insertIfNeed(family, f)
      end
      if table.contains(table.map(members, function (name)
        return "god"..name
      end), Fk.generals[player.deputyGeneral].trueName) then
        table.insertIfNeed(family, f)
      end
    end
  end
  if #family == 0 then return false end
  local ret = {}
  for _, f in ipairs(family) do
    local members = familyMap[f]
    if table.contains(members, Fk.generals[target.general].trueName) then
      table.insertIfNeed(ret, f)
    end
    if target.deputyGeneral ~= "" and table.contains(members, Fk.generals[target.deputyGeneral].trueName) then
      table.insertIfNeed(ret, f)
    end
  end
  if #ret == 0 then return false end
  return ret
end
-- 手牌数变动时触发
jiangu:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(jiangu.name)  and 
    table.find(player.room:getAlivePlayers(), function(p) 
      return familyMember(player, p) and 
      (p:getHandcardNum()>p:getMark("jangu_cardmax") or p:getHandcardNum()<p:getMark("jangu_cardmin"))
    end)
  end,
  on_refresh = function(self, event, target, player, data)
    local room=player.room
    for _, p in ipairs(player.room:getAlivePlayers()) do
      if familyMember(player, p) then
        local hand_count = #p:getCardIds("h")
        if hand_count > p:getMark("jangu_cardmax") then
          room:setPlayerMark(p,"jangu_cardmax",hand_count)
        end
        if hand_count < p:getMark("jangu_cardmin") then
          room:setPlayerMark(p,"jangu_cardmin",hand_count)
        end
      end
    end
  end,
})



-- 记录上轮同族角色手牌数最大值
jiangu:addEffect(fk.RoundEnd, {
  can_refresh= function(self, event, target, player, data)
    return player:hasSkill(jiangu.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local max_hand_count = player:getHandcardNum()
    local min_hand_count = player:getHandcardNum()
    -- 找到同族角色手牌数最大值和最小值
    for _, p in ipairs(room:getAlivePlayers()) do
      if familyMember(player, p) then
        local max_hand_count_p = p:getMark("jangu_cardmax")
        room:setPlayerMark(p,"jangu_cardmax",0)
        local min_hand_count_p=p:getMark("jangu_cardmin")
        room:setPlayerMark(p,"jangu_cardmin",99)

        if max_hand_count_p > max_hand_count then
          max_hand_count = max_hand_count_p
        end
        --
        if min_hand_count_p < min_hand_count then
          min_hand_count = min_hand_count_p
        end

      end
    end
    
    -- 记录最大值
    room:setBanner("@steam__jiangu_max_hand", max_hand_count)
    room:setBanner("@steam__jiangu_min_hand", min_hand_count)
    --room:setPlayerMark(player, "@steam__jiangu_max_hand", max_hand_count)
  end,
})

--检查最大值
local checkjiangu=function(room,player)
  local handcards=#player:getCardIds("h")
  local max_hand_count=room:getBanner("@steam__jiangu_max_hand")
  local min_hand_count=room:getBanner("@steam__jiangu_min_hand")
  if handcards==max_hand_count or handcards==min_hand_count then
    return true
  end
  return false
end
-- 手牌数变动时触发
jiangu:addEffect(fk.AfterCardsMove, {
  can_trigger = function(self, event, target, player, data)
   if player:hasSkill(self.name) and  checkjiangu(player.room,player)
    then 
      for _, move in ipairs(data) do
          if move.to == player and move.toArea == Card.PlayerHand and move.skillName~=jiangu.name then
            return true
          elseif move.from == player  and move.skillName~=jiangu.name then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
        end
      end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCard(player, 1, 1, false, jiangu.name, true, nil, "肩古：请选择要重铸的牌")
    if card then
      room:recastCard(card, player, jiangu.name)
    end
  end,
})

jiangu:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  for _, p in ipairs(room:getAlivePlayers()) do
   
      

    room:setPlayerMark(p,"jangu_cardmax",0)
    room:setPlayerMark(p,"jangu_cardmin",99)

  end
end)

Fk:loadTranslationTable{
  ["steam__jiangu"] = "肩古",
  [":steam__jiangu"] = "宗族技，你的手牌数不因此技能变动至上轮同族角色手牌数到达过的最值后，你可以重铸一张手牌。",
  ["@steam__jiangu_max_hand"] = "肩古最大值",
  ["@steam__jiangu_min_hand"] = "肩古最小值",
}

return jiangu 